package com.mioto.ld.module.organization.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.organization.model.OrgPagerVO;
import com.mioto.ld.module.organization.model.OrgTypeStatisticsVO;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.organization.model.Organization;
import com.mioto.ld.module.organization.service.IOrganizationService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-05-14 19:42:32
 */
@RestController
@RequestMapping("organization")
@Api(tags = "企业信息管理")
public class OrganizationController {
    @Resource
    private IOrganizationService organizationService;

    @GetMapping
    @ApiOperation(value="查询企业信息列表",response = Organization.class)
    public ResultData list (HttpServletRequest request,Organization organization){
        return ResultData.success(organizationService.findList(organization));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询企业信息",response = OrgPagerVO.class)
    public ResultData pager (HttpServletRequest request, Organization organization,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<OrgPagerVO> list = organizationService.findPager(organization);
        PageInfo<OrgPagerVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询企业信息",response = Organization.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(organizationService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增企业信息")
    public ResultData add (Organization organization){
        return Optional.of(organizationService.insert(organization))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(organization))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新企业信息 局部更新,忽略空值")
    public ResultData updateIgnoreNull (Organization organization){
        return Optional.of(organizationService.updateIgnoreNull(organization))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(organization))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新企业信息 整体更新")
    public ResultData update (Organization organization){
        return Optional.of(organizationService.update(organization))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(organization))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除企业信息")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(organizationService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除企业信息")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(organizationService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @GetMapping("/statistics/type")
    @ApiOperation(value="企业类型统计",response = OrgTypeStatisticsVO.class)
    public ResultData typeStatistics(){
        return ResultData.success(organizationService.typeStatistics());
    }
}